package materialdesign.zjitc.net.materialdesign2;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * 步骤
 * 1.添加依赖
 * 2.style代码修改为NoActionBar(Toolbar)
 * 3.定义主布局activity_main.xml
 * 3.创建Fragment相对应的Activity布局(Tablayout)
 * 4.资源文件下创建menu包，并创建mian方法（Toolbar）
 * 5.创建FragmentAdapter（Tablayout）
 * 6.创建每个fragment页面相对应的ListFragment对象（tablayout）
 * 7.定义MainActivity
 */
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTablayout;
private ViewPager mVp;
private List<String> mTitles;
private List<Fragment> mFragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initViews();
    }

    private void init() {
        /**
         * 初始化对象
         */
        mToolbar=findViewById(R.id.toolbae);
        mTablayout=findViewById(R.id.tablayout);
        mVp=findViewById(R.id.vp);
        mTitles=new ArrayList<>();
        mFragments=new ArrayList<>();
    }

    private void initViews() {
        /**
         * Toolbar
         */
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.share:
                        Toast.makeText(MainActivity.this, "分享", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.setting:
                        Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
                        break;
                        default:
                            break;
                }
                return true;
            }
        });
        /**
         * Tablayout
         */
        mTitles.add("头像1");
        mTitles.add("头像2");
        mTitles.add("头像3");
        mTitles.add("头像4");
        ListFragment listFragment=new ListFragment();
        ListFragment2 listFragment2=new ListFragment2();
        ListFragment3 listFragment3=new ListFragment3();
        ListFragment4 listFragment4=new ListFragment4();
        mFragments.add(listFragment);
        mFragments.add(listFragment2);
        mFragments.add(listFragment3);
        mFragments.add(listFragment4);
        FragmentAdapter fragmentAdapter=new FragmentAdapter(getSupportFragmentManager(),mFragments,mTitles);
        mVp.setAdapter(fragmentAdapter);
        mTablayout.setupWithViewPager(mVp);
    }
}
